/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * FontPanel.java
 *
 * Created on Sep 14, 2010, 7:55:24 PM
 */
package project.jpdftools.ui;

import java.awt.Toolkit;
import java.awt.event.KeyEvent;
import java.util.logging.Level;
import javax.swing.JComponent;
import javax.swing.JTextPane;
import project.jpdfexport.constants.JPDFConst.STYLE;
import project.jpdftools.managers.LocalizationManager;
import project.jpdftools.managers.LoggerManager;
import project.jpdftools.other.IntegerTextField;
import project.jpdftools.other.JTextPaneProperties;
import project.jpdftools.ui.logic.JPDF;
import project.jpdftools.ui.logic.JPDFTable;
import project.jpdftools.ui.logic.JPDFText;

/**
 *
 * @author Mido
 */
public class FontPanel extends javax.swing.JDialog {

    private JPDF jpdfComponent;
    private JComponent component;   // UI component that the properties will be applied to it
    private static boolean isComponentVisible = false;

    /**
     * Constructor with parameters
     */
    public FontPanel(JPDF jpdf_component, JComponent component) {
        initComponents();

        this.jpdfComponent = jpdf_component;
        this.component = component;

        // center the component
        project.jpdftools.toolkit.Toolkit.centerWindow(this, this.getHeight(), this.getWidth());
        this.setAlwaysOnTop(true);

        JPDFText text = (JPDFText) jpdf_component;
        // set font size
        String font_size_string = String.valueOf(text.getFontSize());
        list_font_size.setSelectedValue(font_size_string, true);
        tf_font_size.setText(font_size_string);
        // set font style
        String font_style_string = getStyleString(text.getFontStyle());
        list_font_styles.setSelectedValue(font_style_string, true);
        tf_font_style.setText(font_style_string);
        // set font name
        String font_name_string = text.getFontPathName();
        if (font_name_string != null) {
            list_font_names.setSelectedValue(font_name_string, true);
            tf_font_name.setText(font_name_string);
        } else {
            // set default font name
            if (jpdf_component instanceof JPDFText) {
                String component_font_name = ((JPDFText) jpdf_component).getTextPane().getFont().getFontName();
                list_font_names.setSelectedValue(component_font_name, true);
                tf_font_name.setText(component_font_name);
            } else {
                list_font_names.setSelectedValue("Times New Roman", true);
                tf_font_name.setText("Times New Roman");
            }
        }

        // set setAttributes label font
        new JTextPaneProperties(tp_preview_text, Integer.valueOf(tf_font_size.getText()),
                list_font_names.getSelectedValue().toString()).setAttributes(getStyleString(getStyle()));

        // indicate that the frame is visible
        isComponentVisible = true;
        LoggerManager.getLogger().log(Level.INFO, "Frame shown \"{0}\".", "FontPanel");
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        l_font_name = new javax.swing.JLabel();
        l_font_style = new javax.swing.JLabel();
        l_font_size = new javax.swing.JLabel();
        tf_font_name = new javax.swing.JTextField();
        tf_font_style = new javax.swing.JTextField();
        tf_font_size = new IntegerTextField();
        jScrollPane1 = new javax.swing.JScrollPane();
        list_font_names = new javax.swing.JList(project.jpdftools.toolkit.Toolkit.getFontModel());
        jScrollPane2 = new javax.swing.JScrollPane();
        list_font_styles = new javax.swing.JList();
        jScrollPane3 = new javax.swing.JScrollPane();
        list_font_size = new javax.swing.JList();
        p_preview = new javax.swing.JPanel();
        tp_preview_text = new javax.swing.JTextPane();
        bt_ok = new javax.swing.JButton();
        bt_cancel = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle(LocalizationManager.getCurrent_resource().getString("FONT_PANEL_TITLE"));
        setIconImage(Toolkit.getDefaultToolkit().getImage("Data/Icons/Soft Scraps Icons/10.png"));
        setResizable(false);
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                FontPanel.this.windowClosing(evt);
            }
        });

        l_font_name.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        l_font_name.setText(LocalizationManager.getCurrent_resource().getString("FONT_PANEL_L_FONT_NAME"));

        l_font_style.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        l_font_style.setText(LocalizationManager.getCurrent_resource().getString("FONT_PANEL_L_FONT_STYLE"));

        l_font_size.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        l_font_size.setText(LocalizationManager.getCurrent_resource().getString("FONT_PANEL_L_FONT_SIZE"));

        tf_font_name.setEditable(false);
        tf_font_name.setText("Times New Roman");

        tf_font_style.setEditable(false);
        tf_font_style.setText("Normal");

        tf_font_size.setText("12");
        tf_font_size.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                tf_font_sizeKeyPressed(evt);
            }
        });

        list_font_names.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        list_font_names.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                list_font_namesMouseClicked(evt);
            }
        });
        list_font_names.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                list_font_namesKeyPressed(evt);
            }
        });
        jScrollPane1.setViewportView(list_font_names);

        list_font_styles.setModel(new javax.swing.AbstractListModel() {
            String[] strings = { "Normal", "Bold", "Italic", "Bold Italic", "Underlined", "Strikethrough" };
            public int getSize() { return strings.length; }
            public Object getElementAt(int i) { return strings[i]; }
        });
        list_font_styles.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        list_font_styles.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                list_font_stylesMouseClicked(evt);
            }
        });
        jScrollPane2.setViewportView(list_font_styles);

        list_font_size.setModel(new javax.swing.AbstractListModel() {
            String[] strings = { "3", "5", "8", "10", "11", "12", "14", "16", "18", "20", "22", "24", "26", "28", "36", "48", "50" };
            public int getSize() { return strings.length; }
            public Object getElementAt(int i) { return strings[i]; }
        });
        list_font_size.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        list_font_size.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                list_font_sizeMouseClicked(evt);
            }
        });
        jScrollPane3.setViewportView(list_font_size);

        p_preview.setBorder(javax.swing.BorderFactory.createTitledBorder(null, LocalizationManager.getCurrent_resource().getString("FONT_PANEL_L_PREVIEW"), javax.swing.border.TitledBorder.LEFT, javax.swing.border.TitledBorder.TOP, new java.awt.Font("Times New Roman", 1, 12), new java.awt.Color(0, 0, 255))); // NOI18N

        tp_preview_text.setBorder(null);
        tp_preview_text.setEditable(false);
        tp_preview_text.setFont(new java.awt.Font("Segoe UI", 0, 12)); // NOI18N
        tp_preview_text.setText("Sample نموذج");
        tp_preview_text.setOpaque(false);

        javax.swing.GroupLayout p_previewLayout = new javax.swing.GroupLayout(p_preview);
        p_preview.setLayout(p_previewLayout);
        p_previewLayout.setHorizontalGroup(
            p_previewLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(p_previewLayout.createSequentialGroup()
                .addGap(11, 11, 11)
                .addComponent(tp_preview_text, javax.swing.GroupLayout.PREFERRED_SIZE, 504, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(11, Short.MAX_VALUE))
        );
        p_previewLayout.setVerticalGroup(
            p_previewLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(tp_preview_text, javax.swing.GroupLayout.PREFERRED_SIZE, 96, javax.swing.GroupLayout.PREFERRED_SIZE)
        );

        bt_ok.setText(LocalizationManager.getCurrent_resource().getString("OK"));
        bt_ok.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bt_okActionPerformed(evt);
            }
        });

        bt_cancel.setText(LocalizationManager.getCurrent_resource().getString("CANCEL"));
        bt_cancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bt_cancelActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(p_preview, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(l_font_name)
                            .addComponent(tf_font_name)
                            .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(l_font_style)
                            .addComponent(tf_font_style)
                            .addComponent(jScrollPane2))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(tf_font_size, javax.swing.GroupLayout.PREFERRED_SIZE, 172, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jScrollPane3)
                            .addComponent(l_font_size)))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(bt_cancel)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(bt_ok)))
                .addContainerGap())
        );

        layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {jScrollPane1, tf_font_name});

        layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {jScrollPane2, tf_font_style});

        layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {jScrollPane3, tf_font_size});

        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(l_font_name)
                    .addComponent(l_font_style)
                    .addComponent(l_font_size))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(tf_font_name, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(tf_font_style, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(tf_font_size, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 172, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 172, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, 172, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(p_preview, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(bt_ok)
                    .addComponent(bt_cancel))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void bt_cancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bt_cancelActionPerformed
        // close the frame
        this.dispose();
        // indicate that the frame is hidden
        isComponentVisible = false;
        LoggerManager.getLogger().log(Level.INFO, "Frame hidden \"{0}\".", "FontPanel");
    }//GEN-LAST:event_bt_cancelActionPerformed

    private void bt_okActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bt_okActionPerformed
        // apply font on the component
        new JTextPaneProperties(((JTextPane) component), Integer.valueOf(tf_font_size.getText()),
                list_font_names.getSelectedValue().toString()).setAttributes(getStyleString(getStyle()));

        // apply font parameters on the object
        if (jpdfComponent instanceof JPDFText) {
            JPDFText text = (JPDFText) jpdfComponent;
            text.setFontPathName(list_font_names.getSelectedValue().toString());
            text.setFontSize(Integer.valueOf(tf_font_size.getText()));
            text.setFontStyle(getStyle());
        } else if (jpdfComponent instanceof JPDFTable) {
            JPDFTable table = (JPDFTable) jpdfComponent;
            table.setFontPathName(list_font_names.getSelectedValue().toString());
            table.setFontSize(Integer.valueOf(tf_font_size.getText()));
            table.setFontStyle(getStyle());
        }
        // close the frame
        this.dispose();
        // indicate that the frame is hidden
        isComponentVisible = false;
        LoggerManager.getLogger().log(Level.INFO, "Component font properties changed. Font Name = {0}, Font Size = {1}, Font Style = {2}. Component ID = {3}",
                new Object[]{list_font_names.getSelectedValue().toString(), Integer.valueOf(tf_font_size.getText()), getStyleString(getStyle()),
                    jpdfComponent.getId()});
    }//GEN-LAST:event_bt_okActionPerformed

    private void list_font_sizeMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_list_font_sizeMouseClicked
        // apply the selected font size on the textfield
        tf_font_size.setText(list_font_size.getSelectedValue().toString());
        // setAttributes the font size in the setAttributes panel
        new JTextPaneProperties(tp_preview_text, Integer.valueOf(tf_font_size.getText()),
                list_font_names.getSelectedValue().toString()).setAttributes(getStyleString(getStyle()));
    }//GEN-LAST:event_list_font_sizeMouseClicked

    private void tf_font_sizeKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_tf_font_sizeKeyPressed
        if (evt.getKeyChar() == KeyEvent.VK_ENTER) {
            // setAttributes the font size in the setAttributes panel
            new JTextPaneProperties(tp_preview_text, Integer.valueOf(tf_font_size.getText()),
                    list_font_names.getSelectedValue().toString()).setAttributes(getStyleString(getStyle()));
        }
    }//GEN-LAST:event_tf_font_sizeKeyPressed

    private void list_font_stylesMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_list_font_stylesMouseClicked
        // apply the selected font style on the textfield
        tf_font_style.setText(list_font_styles.getSelectedValue().toString());
        // setAttributes the font style in the setAttributes panel
        new JTextPaneProperties(tp_preview_text, Integer.valueOf(tf_font_size.getText()),
                list_font_names.getSelectedValue().toString()).setAttributes(getStyleString(getStyle()));
    }//GEN-LAST:event_list_font_stylesMouseClicked

    private void list_font_namesMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_list_font_namesMouseClicked
        // apply the selected font name on the textfield
        tf_font_name.setText(list_font_names.getSelectedValue().toString());
        // setAttributes the font style in the setAttributes panel
        new JTextPaneProperties(tp_preview_text, Integer.valueOf(tf_font_size.getText()),
                list_font_names.getSelectedValue().toString()).setAttributes(getStyleString(getStyle()));
    }//GEN-LAST:event_list_font_namesMouseClicked

    private void list_font_namesKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_list_font_namesKeyPressed
        if (evt.getKeyChar() == KeyEvent.VK_ENTER) {
            // setAttributes the font style in the setAttributes panel
            new JTextPaneProperties(tp_preview_text, Integer.valueOf(tf_font_size.getText()),
                    list_font_names.getSelectedValue().toString()).setAttributes(getStyleString(getStyle()));
        }
    }//GEN-LAST:event_list_font_namesKeyPressed

    private void windowClosing(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_windowClosing
        bt_cancelActionPerformed(null);
    }//GEN-LAST:event_windowClosing

    /**
     * Returns the selected style from the list as an integer
     *
     * @return
     */
    private int getStyle() {
        String style = list_font_styles.getSelectedValue().toString();
        if (style.equals("Bold") == true) {
            return STYLE.bold;
        } else if (style.equals("Italic") == true) {
            return STYLE.italic;
        } else if (style.equals("Underlined") == true) {
            return STYLE.underlined;
        } else if (style.equals("Strikethrough") == true) {
            return STYLE.strikethrough;
        } else if (style.equals("Bold Italic") == true) {
            return STYLE.bold_italic;
        } else {
            return STYLE.normal;
        }
    }

    /**
     * Returns the string representation of the style
     *
     * @param style the style representation as integer
     * @return
     */
    private String getStyleString(int style) {
        switch (style) {
            case (STYLE.bold):
                return "Bold";
            case (STYLE.italic):
                return "Italic";
            case (STYLE.underlined):
                return "Underlined";
            case (STYLE.strikethrough):
                return "Strikethrough";
            case (STYLE.bold_italic):
                return "Bold Italic";
            default:
                return "Normal";
        }
    }

    /**
     * Returns true if the frame is visible, else return false
     *
     * @return
     */
    public static boolean IsComponentVisible() {
        return isComponentVisible;
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton bt_cancel;
    private javax.swing.JButton bt_ok;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JLabel l_font_name;
    private javax.swing.JLabel l_font_size;
    private javax.swing.JLabel l_font_style;
    private static javax.swing.JList list_font_names;
    private javax.swing.JList list_font_size;
    private javax.swing.JList list_font_styles;
    private javax.swing.JPanel p_preview;
    private javax.swing.JTextField tf_font_name;
    private javax.swing.JTextField tf_font_size;
    private javax.swing.JTextField tf_font_style;
    private javax.swing.JTextPane tp_preview_text;
    // End of variables declaration//GEN-END:variables
}
